<template>
  <div class="home-container">
    <van-nav-bar
        class="page-nav-bar"
        title="标题"
        fixed
    >
      <template #title>
        <van-button
            class="search-btn"
            round
            size="small"
            type="info"
            icon="search"
        >搜索
        </van-button>
      </template>
    </van-nav-bar>
    <!-- animated动画切换-->
    <!-- swipeable开启手势滑动-->
    <!-- active选中的页签-->
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab :key="item.id" v-for="item in channels" :title="item.name">
        <ArticleList :channel="item"/>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="isPopupShow=true">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>

    <!--频道编辑弹出层-->
    <van-popup
        v-model="isPopupShow"
        closeable
        close-icon-position="top-left"
        position="bottom"
        :style="{ height: '90%' }"
    >
      <!--编辑频道的组件-->
      <ChannelEdit @update-active="updateActive" :myChannels="channels" :active="active"/>
    </van-popup>

  </div>
</template>

<script>
import { getChannels } from '@/api/user'
import ArticleList from './components/articleList'
import ChannelEdit from './components/channel-edit'

export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [], // 当前用户的频道数据
      isPopupShow: false
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadChannel()
  },
  mounted () {
  },
  methods: {
    updateActive (index, isShowChannel = false) {
      this.active = index
      this.isPopupShow = isShowChannel
    },
    async loadChannel () {
      try {
        const res = await getChannels()
        this.channels = res?.data?.data?.channels || []
        console.log(this.channels)
      } catch (e) {
        this.$toast('获取频道数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">

.home-container {
  padding-bottom: 100px;
  padding-top: 180px;
}

/deep/ .van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 2;
  width: 100%;
}

// deep的使用场景：
// scoped作用域下，要修改嵌套的子组件的样式，需要添加/deep/属性
/deep/ .van-nav-bar__title {
  max-width: unset;
}

.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 28px;

  .van-icon {
    font-size: 32px;
    color: #fff;
  }
}

/deep/ .channel-tabs {
  .van-tab {
    border-right: 1px solid #EDEFF3;
    min-width: 200px;
    height: 80px;

    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }

  .van-tab--active .van-tab__text {
    color: #333 !important;
  }

  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }

  .van-tabs__nav {
    padding: 0;
  }
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;

  i.toutiao {
    font-size: 33px;
  }
}

.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}

.hamburger-btn {
  position: fixed;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 66px;
  height: 82px;
  background-color: #fff;
  opacity: 0.902;

  i.toutiao {
    font-size: 33px;
  }

  &:before {
    content: "";
    position: absolute;
    left: 0;
    width: 2px;
    height: 100%;
    background-image: url(~@/assets/gradient-gray-line.png);
    background-size: contain;
  }
}
</style>
